/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.business;

import com.sdd.entities.Category;
import com.sdd.entities.Product;
import com.sdd.facades.persistence.CategoryPersistenceFacade;
import com.sdd.facades.persistence.ProductPersistenceFacade;
import com.sdd.plains.CategoryPlain;
import com.sdd.plains.ProductPlain;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 *
 * @author ceu
 */
@Stateless
@LocalBean
public class ProductBusinessFacadeBean {

    @EJB
    ProductPersistenceFacade productPersistenceFacade;
    @EJB
    CategoryPersistenceFacade categoryPersistenceFacade;

    public Product getProduct(Long id) {
        return productPersistenceFacade.find(id);
    }

    public List<Product> getAllProducts() {
        return productPersistenceFacade.findAll();
    }

    public Product addProduct(ProductPlain plain) {
        Product p = productPersistenceFacade.find(plain.getProductId());

        return productPersistenceFacade.create(p);
    }

    public Product addProductToCategory(Product product, Category category) {
        product.addCategory(category);
        return productPersistenceFacade.create(product);
    }

    public Product addProduct(Product entity) {
        return productPersistenceFacade.create(entity);
    }

    public Product addProductToCategory(Long idprod, Long idcat) {
        Product p = productPersistenceFacade.find(idprod);
        Category c = categoryPersistenceFacade.find(idcat);
        p.addCategory(c);
        return productPersistenceFacade.create(p);
    }

    public Product addProductToCategory(ProductPlain plain, List<CategoryPlain> l) {

        Product p = productPersistenceFacade.find(plain.getProductId());
        List<Category> categoriesOld = p.getCategories();
        p.setCategories(null);
        p = productPersistenceFacade.create(p);
        for (CategoryPlain categoryPlain : l) {
            p.addCategory(categoryPersistenceFacade.find(categoryPlain.getCategoryId()));
   }
        /*for (Category category : categoriesOld) {
        if(!p.getCategories().contains(category)){
        p.addCategory(category);
        }
        }*///NO TIENE SENTIDO???
        return productPersistenceFacade.create(p);

    }

    public List<Product> getProductsByCategory(CategoryPlain cp) {
        Category c = categoryPersistenceFacade.find(cp.getCategoryId());
        return productPersistenceFacade.getProductsByCategory(c);
    }
    
    public List<Product> getProductsWithoutCategory() {
        return productPersistenceFacade.getProductsWithoutCategory();
    }
    
    public List<Product> getProducts(int from, int many){
        int array[]=new int[2];
        array[0]=from;
        array[1]=from+many;
        return productPersistenceFacade.findRange(array);
    }
    public List<Product> getProductsWithoutCategory(int from, int many) {
        int array[]=new int[2];
        array[0]=from;
        array[1]=from+many;
        return productPersistenceFacade.getProductsWithoutCategory(array);
    }
    
    public void removeProduct(Product p){
        productPersistenceFacade.disable(p);
    }
}
